package com.douyang.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.douyang.dto.PayOrderDTO;
import com.douyang.mapper.MqMessageMapper;

import com.douyang.model.MqMessage;
import com.douyang.service.MqMessageService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2025-03-20
 */
@Service
public class MqMessageServiceImpl extends ServiceImpl<MqMessageMapper, MqMessage> implements MqMessageService {

    @Override
    public void saveFailToDB(PayOrderDTO payOrderDTO) {
        String jsonStr = JSONUtil.toJsonStr(payOrderDTO);
        MqMessage mqMessage = lambdaQuery().eq(MqMessage::getDataJson, jsonStr).one();
        if (mqMessage == null) {
            mqMessage=new MqMessage();
            mqMessage.setCreateTime(LocalDateTime.now());
            mqMessage.setDataJson(jsonStr);
            mqMessage.setFailCount(1);
        }else{
            mqMessage.setFailCount(mqMessage.getFailCount()+1);
            mqMessage.setUpdateTime(LocalDateTime.now());
        }
        saveOrUpdate(mqMessage);
    }
}
